package com.whpost.service.dao.cwqf;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.whpost.service.Vo.JgkbItem;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

public interface JgkbMapper extends BaseMapper<JgkbItem> {

    /**
     * 查询指定层级、指定日期、指定来源的欠费看板数据
     * 当 pcode 为空时，汇总所有下级；否则仅汇总 pcode 下的网点
     *
     * @param rq      业务日期
     * @param source  数据来源  jd / yw
     * @param pcode   上级机构代码(可为 null)
     * @param jb      机构级别 2-区局 3-网点
     */
    @Select({
            "<script>",
            "SELECT j.jgdm  AS code,",
            "       j.name  AS name,",
            "       SUM(v.yqqf) AS postArrears,",
            "       SUM(v.hj)   AS postOverdue",
            "  FROM hs_jg j",
            "  JOIN vw_cwqf v ON v.jg = j.jgdm",
            " WHERE j.jb = #{jb} and j.zybz='1'",
            "   AND TO_CHAR(v.rq, 'yyyyMMdd') = #{rq})",
            "   AND v.data_source = #{source}",
            "   <if test='pcode != null and pcode != \"\"'>",
            "     AND j.pcode = #{pcode}",
            "   </if>",
            " GROUP BY j.jgdm, j.name",
            "</script>"
    })
    List<JgkbItem> selectBranchData(@Param("rq") String rq,
                                    @Param("source") String source,
                                    @Param("pcode") String pcode,
                                    @Param("jb") Integer jb);
}